import re

HERO_NAME_PATH = 'name.txt'
HERO_WEAPON_PATH = 'weapon.txt'
THREE_KINGDOM_PATH = 'sanguo.txt'


# 统计出现的次数
def find_items(hero):
    with open(THREE_KINGDOM_PATH, encoding='GB18030') as f:
        data = f.read().replace('\n', '')
        name_num = len(re.findall(hero, data))
    return name_num


# 读取英雄人物的信息
name_dict = {}
with open(HERO_NAME_PATH) as f:
    for line in f:
        names = line.split('|')
        for name in names:
            name_num = find_items(name)
            name_dict[name] = name_num;
#print(name_dict)
# 以value从大到小排序
name_sorted = sorted(name_dict.items(), key=lambda item: item[1], reverse=True)
print(name_sorted[0:5])